﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using BadmintonApp.Entity;
using BadmintonApp.DAO.Interface;

namespace BadmintonApp.DAO.Implement
{
    public class RoleDAO :  BaseDAO, IRoleDAO
    {
        /// <summary>
        /// 获取最大的角色ID
        /// </summary>
        /// <returns></returns>
        private int GetMaxRoleID()
        {
            string sql = "Select IsNull(Max(FRoleID), 0) from TRoles";
            DbCommand selectCmd = db.GetSqlStringCommand(sql);
            return (int)db.ExecuteScalar(selectCmd);
        }

        #region 

        /// <summary>
        /// 插入角色
        /// </summary>
        /// <param name="rolesInfo">角色信息类</param>
        public void InsertRole(TRolesInfo rolesInfo)
        {
            StringBuilder sbSQL = new StringBuilder();
            sbSQL.Append("Insert into TRoles(FRoleID, FRoleName, FMemo) Values (@FRoleID, @FRoleName, @FMemo)");
            DbCommand insertCmd = db.GetSqlStringCommand(sbSQL.ToString());

            rolesInfo.FRoleID = GetMaxRoleID() + 1;
            db.AddInParameter(insertCmd, "FRoleID", DbType.Int16, rolesInfo.FRoleID);
            db.AddInParameter(insertCmd, "FRoleName", DbType.String, rolesInfo.FRoleName);
            db.AddInParameter(insertCmd, "FMemo", DbType.String, rolesInfo.FMemo);

            db.ExecuteNonQuery(insertCmd);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="rolesInfo">角色信息类</param>
        public void UpdateRole(TRolesInfo rolesInfo)
        {
            StringBuilder sbSQL = new StringBuilder();
            sbSQL.Append("Update TRoles set FRoleName=@FRoleName, FMemo=@FMemo Where FRoleID=@FRoleID");

            DbCommand updateCmd = db.GetSqlStringCommand(sbSQL.ToString());
            db.AddInParameter(updateCmd, "FRoleID", DbType.Int32, rolesInfo.FRoleID);
            db.AddInParameter(updateCmd, "FRoleName", DbType.String, rolesInfo.FRoleName);
            db.AddInParameter(updateCmd, "FMemo", DbType.String, rolesInfo.FMemo);
            db.ExecuteNonQuery(updateCmd);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleID">角色ID</param>
        public void DeleteRole(int roleID)
        {
            string sql = "Delete from TRoles Where FRoleID=@FRoleID";
            DbCommand deleteCmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(deleteCmd, "FRoleID", DbType.Int32, roleID);
            db.ExecuteNonQuery(deleteCmd);
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns>角色信息类列表</returns>
        public DataSet GetAllRoles()
        {
            string sql = "Select * from TRoles";
            DbCommand selectCmd = db.GetSqlStringCommand(sql);

            DataSet dsRoles = db.ExecuteDataSet(selectCmd);
            return dsRoles;
        }

        /// <summary>
        /// 通过名称获取角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns>角色信息类</returns>
        public TRolesInfo GetRoleByName(string roleName)
        {
            string sql = "Select * from TRoles Where FRoleName=@FRoleName";
            TRolesInfo roleInfo = new TRolesInfo();
            DbCommand selectCmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(selectCmd, "FRoleName", DbType.String, roleName);
            DataSet dsRoles = db.ExecuteDataSet(selectCmd);
            foreach(DataRow dr in dsRoles.Tables[0].Rows)
            {
                roleInfo.FRoleID = Convert.ToInt32(dr["FRoleID"]);
                roleInfo.FRoleName = dr["FRoleName"].ToString();
                roleInfo.FMemo = dr["FMemo"].ToString();
            }
            return roleInfo;
        }

        #endregion
    }
}
